package com.fau.wmbj.server.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.fau.wmbj.server.entity.Company;
import com.fau.wmbj.server.entity.Menu;
import com.fau.wmbj.server.service.CompanyService;
import com.fau.wmbj.server.service.MenuService;
import com.fau.wmbj.server.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 单位管理
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2024-03-18 9:08
 */
@Service
public class CompanyServiceImpl extends BaseServiceImpl<Company, Long> implements CompanyService {

    @Resource
    private MenuService menuService;

    @Override
    public void update(Company company, List<Long> ids) {
        Long id = company.getId();
        Company before = super.find(id);
        Set<Menu> menuSet = new HashSet<>();
        if (CollectionUtil.isNotEmpty(ids)) {
            List<Menu> menuList = menuService.findList(ids);
            menuSet.addAll(menuList);
        }
        before.setMenus(menuSet);
        before.setAddress(company.getAddress());
        before.setDesc_(company.getDesc_());
        before.setName(company.getName());
        before.setEmail(company.getEmail());
        before.setPhone(company.getPhone());
        super.update(before);
    }

    @Override
    public void save(Company company, List<Long> ids) {
        Set<Menu> menuSet = new HashSet<>();
        if (CollectionUtil.isNotEmpty(ids)) {
            List<Menu> menuList = menuService.findList(ids);
            menuSet.addAll(menuList);
        }
        company.setMenus(menuSet);
        super.save(company);
    }
}
